package com.onepower.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onepower.core.domain.request.CreateMatchScheduleForm;
import com.onepower.core.domain.request.MatchScheduleQuery;
import com.onepower.core.domain.response.MatchScheduleInfo;
import com.onepower.core.domain.response.MatchScoreInfo;
import com.onepower.core.modules.repository.entity.MatchReferee;
import com.onepower.core.modules.service.IMatchRefereeService;
import com.onepower.core.modules.service.IMatchScheduleService;
import com.onepower.core.modules.service.IMatchScoreService;
import com.onepower.core.result.BizResultCode;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.DateUtils;
import com.onepower.core.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 比赛场次表 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-06
 */
@RestController
@RequestMapping("api/match/schedule")
public class MatchScheduleController {

    @Autowired
    private IMatchScheduleService matchScheduleService;

    @Autowired
    private IMatchScoreService matchScoreService;

    @Autowired
    private IMatchRefereeService matchRefereeService;

    @PostMapping("import")
    public CommonResult importSchedule(@RequestParam("matchId") Integer matchId,
                                       @RequestParam("file") MultipartFile file) throws Exception {
        return matchScheduleService.createScheduleByImport(matchId, file);
    }

    @RequestMapping(value = "createScheduleByOnline", method = RequestMethod.POST)
    public CommonResult createScheduleByOnline(@RequestBody List<CreateMatchScheduleForm> forms) throws IOException {
        return matchScheduleService.createScheduleServiceByOnline(forms);
    }

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public CommonResult getSchedule(
            @RequestParam(value = "matchScheduleId", required = false, defaultValue = "-1") int matchScheduleId) {
        MatchScheduleInfo schedule = matchScheduleService.getSchedule(matchScheduleId);
        return ResultUtils.ok(schedule);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "matchId", required = false, defaultValue = "-1") int matchId,
                             @RequestParam(value = "matchPlaceId", required = false, defaultValue = "-1") int matchPlaceId,
                             @RequestParam(value = "scheduleLevel", required = false, defaultValue = "") String scheduleLevel,
                             @RequestParam(value = "scheduleGroup", required = false, defaultValue = "") String scheduleGroup,
                             @RequestParam(value = "scheduleName", required = false, defaultValue = "") String scheduleName,
                             @RequestParam(value = "state", required = false, defaultValue = "-1") int state,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        MatchScheduleQuery query = new MatchScheduleQuery();
        query.setMatchId(matchId);
        query.setMatchPlaceId(matchPlaceId);
        query.setScheduleLevel(scheduleLevel);
        query.setScheduleGroup(scheduleGroup);
        query.setScheduleName(scheduleName);
        query.setState(state);
        Page<MatchScheduleInfo> scheduleList = matchScheduleService.getScheduleList(new Page<>(pageNum, pageSize), query);
        return ResultUtils.ok(scheduleList);
    }

    @RequestMapping(value = "getMatchScoreList", method = RequestMethod.GET)
    public CommonResult getMatchScoreList(
            @RequestParam(value = "matchScheduleId", required = false, defaultValue = "-1") int matchScheduleId) {
        List<MatchScoreInfo> matchScoreInfoList = matchScoreService.getMatchScoreList(matchScheduleId);
        return ResultUtils.ok(matchScoreInfoList);
    }

    @RequestMapping(value = "getLockMatch", method = RequestMethod.GET)
    public CommonResult getLockMatch(@RequestParam int matchId,
                                     @RequestParam int matchAdminId) {
        MatchScheduleInfo matchScheduleInfo = matchScheduleService.getMatchScheduleLockedByReferee(matchId, matchAdminId);
        return ResultUtils.ok(matchScheduleInfo);
    }


    @RequestMapping(value = "updateTeamAndName", method = RequestMethod.POST)
    public CommonResult updateTeamAndName(@RequestBody MatchScheduleInfo matchScheduleInfo) {
        Map map = new HashMap();
        map.put("matchScheduleId", matchScheduleInfo.getId());
        map.put("blueTeam", matchScheduleInfo.getBlueTeam());
        map.put("bluePlayer", matchScheduleInfo.getBluePlayer());
        map.put("redTeam", matchScheduleInfo.getRedTeam());
        map.put("redPlayer", matchScheduleInfo.getRedPlayer());
        return matchScheduleService.updateMatchSchedule(map);
    }

    @RequestMapping(value = "updateScore", method = RequestMethod.POST)
    public CommonResult updateScore(@RequestBody MatchScheduleInfo matchScheduleInfo) {
        Map map = new HashMap();
        map.put("matchScheduleId", matchScheduleInfo.getId());
        map.put("expressivenessScore", matchScheduleInfo.getExpressivenessScore());
        map.put("accuracyScore", matchScheduleInfo.getAccuracyScore());
        map.put("totalScore", matchScheduleInfo.getTotalScore());
        return matchScheduleService.updateMatchSchedule(map);
    }

    @RequestMapping(value = "updatePsEntry", method = RequestMethod.GET)
    public CommonResult updatePsEntry(@RequestParam(value = "psEntry") String psEntry, int matchScheduleId) {
        Map map = new HashMap();
        map.put("matchScheduleId", matchScheduleId);
        map.put("psEntry", psEntry);
        return matchScheduleService.updateMatchSchedule(map);
    }

    @RequestMapping(value = "showScore", method = RequestMethod.GET)
    public CommonResult showScore(@RequestParam(value = "matchScheduleId") int matchScheduleId) {
        Map map = new HashMap();
        map.put("matchScheduleId", matchScheduleId);
        map.put("isShowScore", "");
        return matchScheduleService.updateMatchSchedule(map);
    }

    @RequestMapping(value = "showRanking", method = RequestMethod.GET)
    public CommonResult showRanking(@RequestParam(value = "matchScheduleId") int matchScheduleId) {
        Map map = new HashMap();
        map.put("matchScheduleId", matchScheduleId);
        map.put("isShowRanking", "");
        return matchScheduleService.updateMatchSchedule(map);
    }

    @ResponseBody
    @RequestMapping(value = "checkReferee", method = RequestMethod.GET)
    public CommonResult checkReferee(@RequestParam int refereeId) {
        MatchReferee matchReferee = matchRefereeService.getById(refereeId);
        Long diffMin = DateUtils.sub2Min(matchReferee.getCheckTime(),new Date());
        return diffMin > 1 ? ResultUtils.error(BizResultCode.REFEREE_DISCONNECT) : ResultUtils.ok();
    }
}
